package xj.graph2d.app.prop;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import xj.app.DrawToolkit;
import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.resources.SVGIcon;
import xj.graph2d.shapes.SVGShape;
import xj.graph2d.tools.OnePointShapeTool;
import xj.util.gui.MultiExtensionsFileFilter;

public class SVGShapeListener 
  extends xj.graph2d.app.DrawAction 
  implements ActionListener {

  public SVGShapeListener() {
    FileFilter svgFileFilter = new MultiExtensionsFileFilter(extensions,
							     "SVG files (.svg .svgz)");
    chooser = new JFileChooser();
    chooser.addChoosableFileFilter(svgFileFilter);
    previewer = new FilePreviewer(chooser);
    chooser.setAccessory(previewer);
  }

  public void setCurrentDirectory(String dir) {
    if (dir != null) {
      chooser.setCurrentDirectory(new File(dir));
    }
  }

  public void actionPerformed(ActionEvent e) {
    previewer.loadImage(null);
    chooser.rescanCurrentDirectory();
    int retval = chooser.showOpenDialog(null);
    if (retval == JFileChooser.APPROVE_OPTION) {
      File theFile = chooser.getSelectedFile();
      if (theFile != null) {
	if (theFile.isFile()) {
	  String filename = chooser.getSelectedFile().getAbsolutePath();
	  try {
	    SVGShape svgShape = new SVGShape(new File(filename), true);
	    SimpleDrawView view = main.getView();
	    SimpleDrawDocument doc = main.getDoc();
	    DrawToolkit toolkit = main.getDrawToolkit();
	    OnePointShapeTool tool = (OnePointShapeTool) toolkit.getTool("SVGTool");
	    if (tool == null) { 
	      tool = new OnePointShapeTool(svgShape,
					   DrawShapeConstants.CLONE_PROTOTYPE_DRAW_ATTR);
	      main.addTool("SVGTool", tool);
	    } else { 
	      tool.setPrototype(svgShape);
	    }
	    view.setCurrentTool(tool);
	    DrawCanvas canvas = view.getCanvas();
	    Point p = canvas.getMousePosition();
	    tool.mouseMoved(view, p, 0);
	  } catch (IOException ex) {
	    System.out.println("Error: reading SVG file " + ex.getMessage());
	  }
	}
      }
    }
  }

  protected String[] extensions = { "svg", "svgz" };

  protected JFileChooser chooser;

  protected FilePreviewer previewer;

  static int id = 1; 

  class FilePreviewer extends JComponent implements PropertyChangeListener {

    SVGIcon icon;

    public FilePreviewer(JFileChooser fc) {
      setPreferredSize(new Dimension(100, 100));
      fc.addPropertyChangeListener(this);
    }

    public void loadImage(File f) {
      //System.out.println("SVGShapeListener.loadImage() f=" + f);
      if (f == null) {
	icon = null; 
      } else {
	boolean zipped = (f.getName().endsWith(".svgz"));
	try { 
	  FileInputStream in = new FileInputStream(f);
	  Dimension dim = new Dimension(100, 100);
	  icon = new SVGIcon(in, zipped, dim);
	} catch (IOException e) {
	  e.printStackTrace();
	}
      }
    }

    public void propertyChange(PropertyChangeEvent e) {
      String prop = e.getPropertyName();
      if (prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
	if (isShowing()) {
	  loadImage((File) e.getNewValue());
	  repaint();
	}
      }
    }

    @Override
    public void paint(Graphics g) {
      if (icon != null) {
	int x = getWidth() / 2 - icon.getIconWidth() / 2;
	int y = getHeight() / 2 - icon.getIconHeight() / 2;
	if (y < 0) {
	  y = 0;
	}

	if (x < 5) {
	  x = 5;
	}

	icon.paintIcon(this, g, 0, 0);
      }
    }
  }

}
